package com.example.wwk.graduationpractice.AsyncTask;

import android.content.Context;
import android.os.AsyncTask;

import com.example.wwk.graduationpractice.NetDataUtils.GroupUtils;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.Globalutils.CustomDialogUtils;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;
import com.example.wwk.graduationpractice.SmackUtils.XmppConnectUtils;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.packet.Presence;


import java.io.IOException;

/**
 * Created by wwk on 2017/3/17.
 */

public class RegisterAsyncTask {
    public static void register(final Context context, final String username, final String pwd, final String email) {//    注册按钮事件

        AsyncTask<String, String, Boolean> execute = new AsyncTask<String, String, Boolean>() {//传入任务的参数类型；显示进度的单位；返回结果类型
            private CustomDialogUtils progressDialog;
            //smack 消息通道
            private AbstractXMPPConnection connection;
            private ChatManager chatManager;
            private Chat chat;
            //发送的内容及接收者
            private String content = username + "#" + pwd + "&" + email;
            private String registerReceiver = "register";
            private String testUser = "test";
            private String groupName = "user";
            private String testPwd = "123";


            protected void onPreExecute() {              //任务开始前执行，一些初始化操作
                progressDialog = new CustomDialogUtils(context, R.style.CustomDialog);
                progressDialog.setCancelable(false);        //无法手动取消
                progressDialog.show();
            }

            @Override
            protected Boolean doInBackground(String... strings) {

                connection = XmppConnectUtils.getConnection();
                try {
                    connection.login(testUser, testPwd);
                } catch (XMPPException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                chatManager = ChatManager.getInstanceFor(connection);      //获取实例
                chat = chatManager.createChat(registerReceiver + "@" + connection.getServiceName());   //为发送信息(问题)建立聊天通道
                //判断注册的用户是否已经存在
                if ( GroupUtils.getAlreadyUser(username, groupName).getAlreadyUser()==1)
                    return false;
                else {     //当不存在这个用户
                    try {
                        chat.sendMessage(content);
                    } catch (SmackException.NotConnectedException e) {
                        e.printStackTrace();
                        return false;
                    }
                }

                return true;

            }

            @Override
            protected void onPostExecute(Boolean bool) {
                progressDialog.dismiss();  //取消对话框
                Presence presence = new Presence(Presence.Type.available);
                try {
                    connection.disconnect(presence);
                    connection = null;
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
                if (bool.equals(true)) {
                    PromptShowUtils.showToast("注册成功！");
                } else
                    PromptShowUtils.showDialog("提示", "用户名已存在！", context);
            }
        }.execute();
    }

}
